﻿#include <QDateTime>
#include "CAlarmRecordQuery.h"
#include "CGlobal.h"
#include "Header.h"
#include "CSqlOper.h"
#include "CAlarmDetailsDlg.h"
#include "CVideoResultQuery.h"
#include "Public.h"

CAlarmRecordQuery::CAlarmRecordQuery(QWidget *parent)
	: QDialog(parent)
{
	ui.setupUi(this);
	setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
	this->setWindowTitle(A2T("报警记录查询"));
	// 获取 Qt 窗口的原生 HWND 句柄
	HWND hwnd = reinterpret_cast<HWND>(this->winId());
	setWindowsTitleBarColor(hwnd, RGB(56, 63, 81));
	m_nPerPageCount = 100;//默认每页显示100条数据
	m_nCurrentPage = 1;//当前页数第一页
	m_nTotalCount = 0;//记录总数
	m_nPageCount = 0;//总页数
	setStyleSheetFile();
	initConnect();
	initData();
	ui.pushButtonExport->hide();
	slotPushButtonQuery();
}

CAlarmRecordQuery::~CAlarmRecordQuery()
{
	
}

void CAlarmRecordQuery::initConnect()
{
	connect(ui.pushButtonQuery, &QPushButton::clicked, this, &CAlarmRecordQuery::slotPushButtonQuery);
	connect(ui.pushButtonDelDay, &QPushButton::clicked, this, &CAlarmRecordQuery::slotDelDay);
	connect(ui.pushButtonAddDay, &QPushButton::clicked, this, &CAlarmRecordQuery::slotAddDay);
	connect(ui.pushButtonUp, &QPushButton::clicked, this, &CAlarmRecordQuery::slotUp);
	connect(ui.pushButtonDown, &QPushButton::clicked, this, &CAlarmRecordQuery::slotDown);
	connect(ui.pushButtonExport, &QPushButton::clicked, this, &CAlarmRecordQuery::slotExport);
	connect(ui.pushButtonResult, &QPushButton::clicked, this, &CAlarmRecordQuery::slotResult);
	connect(ui.widget, SIGNAL(sigShowImg(QString)), this, SLOT(slotShowImg(QString)));
}

void CAlarmRecordQuery::setStyleSheetFile()
{
	QString sQssPath = ":/qss/styleSheet.css";
	QFile qss(sQssPath);
	qss.open(QFile::ReadOnly);
	this->setStyleSheet(qss.readAll());
}

void CAlarmRecordQuery::initData()
{
	ui.comboBoxAlarmType->addItem(A2T("*"), -1);
	ui.comboBoxAlarmType->addItem(A2T("安全帽反光衣"),enSafetyHelmet);
	ui.comboBoxAlarmType->addItem(A2T("堆煤煤流占比"), enCoalFlow);
	ui.comboBoxAlarmType->addItem(A2T("皮带异物"), enBeltForeign);
	ui.comboBoxAlarmType->addItem(A2T("大块煤检测"), enLargeCoal);
	ui.comboBoxAlarmType->addItem(A2T("皮带跑偏算法"), enBeltDeviation);
	ui.comboBoxAlarmType->addItem(A2T("烟雾火点"), enSmokeFire);
	ui.comboBoxAlarmType->addItem(A2T("抽烟打电话"), enSmokeMakeCall);
	ui.comboBoxAlarmType->addItem(A2T("测温报警"), enTemp);
	ui.comboBoxAlarmType->addItem(A2T("周界防范"), enPerimeter);
	ui.comboBoxAlarmType->addItem(A2T("传送带堵料"), enBlock);
	ui.comboBoxAlarmType->addItem(A2T("抛洒滴漏"), enThrow);
	ui.comboBoxAlarmType->addItem(A2T("街面行为"), enStreet);

	m_mapAlarmType.insert(-1, "*");
	m_mapAlarmType.insert(enSafetyHelmet, A2T("安全帽反光衣"));
	m_mapAlarmType.insert(enCoalFlow, A2T("堆煤煤流占比"));
	m_mapAlarmType.insert(enBeltForeign, A2T("皮带异物"));
	m_mapAlarmType.insert(enLargeCoal, A2T("大块煤检测"));
	m_mapAlarmType.insert(enBeltDeviation, A2T("皮带跑偏算法"));
	m_mapAlarmType.insert(enSmokeFire, A2T("烟雾火点"));
	m_mapAlarmType.insert(enSmokeMakeCall, A2T("抽烟打电话"));
	m_mapAlarmType.insert(enTemp, A2T("测温报警"));
	m_mapAlarmType.insert(enPerimeter, A2T("周界防范"));
	m_mapAlarmType.insert(enBlock, A2T("传送带堵料"));
	m_mapAlarmType.insert(enThrow, A2T("抛洒滴漏"));
	m_mapAlarmType.insert(enStreet, A2T("街面行为"));

	ui.comboBoxCamerName->addItem(A2T("*"), "*");
	QString sErrMsg = "";
	QVector<QSqlRecord> vecInfo;
	CSqlOper::instance().selectCamerName(vecInfo,sErrMsg);
	for (int i = 0; i < vecInfo.size(); i++)
	{
		ui.comboBoxCamerName->addItem(vecInfo[i].value("DEV_NAME").toString(), vecInfo[i].value("DEV_GUID").toString());
	}
	//设置两时间编辑栏
	QDateTime timeTo = QDateTime::currentDateTime();
	timeTo.setTime(QTime::fromString("23:59:59", "hh:mm:ss"));
	QDateTime timeFrom = QDateTime::currentDateTime();
	timeFrom.setTime(QTime::fromString("00:00:00", "hh:mm:ss"));
	ui.dateTimeEditBegin->setDateTime(timeFrom);
	ui.dateTimeEditEnd->setDateTime(timeTo);
	ui.dateTimeEditBegin->setCalendarPopup(true);
	ui.dateTimeEditEnd->setCalendarPopup(true);
	ui.widget->InitUi(2);
}

void CAlarmRecordQuery::slotPushButtonQuery()
{
	QVector<QSqlRecord> vecRecord;
	QString sErrMsg = "";
	QString sStartTime = ui.dateTimeEditBegin->dateTime().toString("yyyy/MM/dd hh:mm:ss");
	QString sEndTime = ui.dateTimeEditEnd->dateTime().toString("yyyy/MM/dd hh:mm:ss");
	QString sAlarmType = m_mapAlarmType[ui.comboBoxAlarmType->currentData().toInt()];
	QString sDevGuid = ui.comboBoxCamerName->currentData().toString();
	CSqlOper::instance().selectAlarmResult(sDevGuid, sAlarmType, sStartTime, sEndTime, vecRecord, sErrMsg);
	m_nTotalCount = vecRecord.size();
	if (m_nTotalCount % m_nPerPageCount == 0)
	{
		m_nPageCount = m_nTotalCount / m_nPerPageCount;
	}
	else
	{
		m_nPageCount = m_nTotalCount / m_nPerPageCount + 1;
	}
	if (m_nCurrentPage > m_nPageCount)
	{
		m_nCurrentPage = 1;
	}
	ui.labelPage->setText(A2T("%1/%2页").arg(m_nCurrentPage).arg(m_nPageCount));
	if (m_nCurrentPage == 1)
	{
		ui.pushButtonUp->setEnabled(false);
	}
	else
	{
		ui.pushButtonUp->setEnabled(true);
	}
	if (m_nCurrentPage == m_nPageCount)
	{
		ui.pushButtonDown->setEnabled(false);
	}
	else
	{
		ui.pushButtonDown->setEnabled(true);
	}
	//计算只展示的内容
	int nStart = (m_nCurrentPage - 1) * m_nPerPageCount;//第一页0-99 第二页100-199 共101条
	int nEnd = (m_nCurrentPage - 1) * m_nPerPageCount + m_nPerPageCount;
	if (nEnd > m_nTotalCount)
	{
		nEnd = m_nTotalCount;
	}
	QVector<QVector<QString>>vecStr;
	vecStr.clear();
	for (int i = nStart; i < nEnd; i++)
	{
		QVector<QString> vecRow;
		vecRow.push_back(QString::number(i));//序号
		vecRow.push_back(vecRecord[i].value("DEV_NAME").toString());//摄像头名
		vecRow.push_back(vecRecord[i].value("EVENT_TYPE").toString());//报警类型
		vecRow.push_back(vecRecord[i].value("ALARM_TIME").toDateTime().toString("yyyy/MM/dd hh:mm:ss"));//报警时间
		vecRow.push_back(vecRecord[i].value("ALARM_LEVEL").toString());//报警等级
		if (vecRecord[i].value("IS_CONFIRM").toString() == "1")
		{
			vecRow.push_back(A2T("是"));//是否确认
		}
		else if (vecRecord[i].value("IS_CONFIRM").toString() == "0")
		{
			vecRow.push_back(A2T("否"));//是否确认
		}
		vecRow.push_back(vecRecord[i].value("GUID").toString());//Guid
		vecStr.push_back(vecRow);
	}

	ui.widget->InitData(2, vecStr);
}

void CAlarmRecordQuery::slotDelDay()
{
	QDateTime timeTo = ui.dateTimeEditBegin->dateTime().addDays(-1);
	timeTo.setTime(QTime::fromString("23:59:59", "hh:mm:ss"));
	QDateTime timeFrom = ui.dateTimeEditBegin->dateTime().addDays(-1);
	timeFrom.setTime(QTime::fromString("00:00:00", "hh:mm:ss"));
	ui.dateTimeEditBegin->setDateTime(timeFrom);
	ui.dateTimeEditEnd->setDateTime(timeTo);
	slotPushButtonQuery();
}

void CAlarmRecordQuery::slotAddDay()
{
	QDateTime timeTo = ui.dateTimeEditBegin->dateTime().addDays(1);
	timeTo.setTime(QTime::fromString("23:59:59", "hh:mm:ss"));
	QDateTime timeFrom = ui.dateTimeEditBegin->dateTime().addDays(1);
	timeFrom.setTime(QTime::fromString("00:00:00", "hh:mm:ss"));
	ui.dateTimeEditBegin->setDateTime(timeFrom);
	ui.dateTimeEditEnd->setDateTime(timeTo);
	slotPushButtonQuery();
}

void CAlarmRecordQuery::slotUp()
{
	if (m_nCurrentPage > 1)
	{
		m_nCurrentPage--;
	}
	slotPushButtonQuery();
}

void CAlarmRecordQuery::slotDown()
{
	if (m_nCurrentPage < m_nPageCount)
	{
		m_nCurrentPage++;
	}
	slotPushButtonQuery();
}

void CAlarmRecordQuery::slotExport()
{

}

void CAlarmRecordQuery::slotShowImg(QString sAlarmGuid)
{
	CAlarmDetailsDlg oper(sAlarmGuid);
	oper.exec();
	slotPushButtonQuery();
}

void CAlarmRecordQuery::slotResult()
{
	CVideoResultQuery oper;
	oper.exec();
}
